<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Think\Session\Driver;

class Redis {

    protected $lifeTime = 3600;
    protected $sessionName = '';
    protected $handle = null;

    /**
     * 打开Session 
     * @access public 
     * @param string $savePath 
     * @param mixed $sessName  
     */
    
    public function open($savePath, $sessName) {
        $this->lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime;
        $this->handle = new \Redis();
        $hosts= C('REDIS_HOST');
        $ports= C('REDIS_PORT');
        $this->handle->connect($hosts, $ports);
        return true;
    }

    /**
     * 关闭Session 
     * @access public 
     */
    public function close() {
        $this->gc(ini_get('session.gc_maxlifetime'));
        $this->handle->close();
        $this->handle = null;
        return true;
    }

    /**
     * 读取Session 
     * @access public 
     * @param string $sessID 
     */
    public function read($sessID) {
        $this->sessionName = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->sessionName;
        return $this->handle->get($this->sessionName . $sessID);
    }

    /**
     * 写入Session 
     * @access public 
     * @param string $sessID 
     * @param String $sessData  
     */
    public function write($sessID, $sessData) {
        $this->sessionName = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->sessionName;
        return $this->handle->set($this->sessionName . $sessID, $sessData, $this->lifeTime);
    }

    /**
     * 删除Session 
     * @access public 
     * @param string $sessID 
     */
    public function destroy($sessID) {
        return $this->handle->delete($this->sessionName . $sessID);
    }

    /**
     * Session 垃圾回收
     * @access public 
     * @param string $sessMaxLifeTime 
     */
    public function gc($sessMaxLifeTime) {
        return true;
    }

    //put your code here
}
